package com.vercon.moensmart.adapter

import android.app.Activity
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.vercon.common.utils.LogUtils
import com.vercon.moensmart.adapter.base.BaseAdapter
import com.vercon.moensmart.databinding.ItemAppBinding
import com.vercon.moensmart.fragment.AppFragment
import com.vercon.moensmart.mode.AppMode
import com.vercon.moensmart.utils.MenuImageUtil

class AppAdapter(context: Activity) : BaseAdapter<ItemAppBinding, AppMode>(context) {
    private var isShowDelete = false //卸载按钮是否显示
    private var mOnItemDeleteListener: OnItemDeleteListener? = null

    // 正在卸载
    private var isDeleting = false //是否正在卸载
    private var pm: PackageManager? = null

    fun setDeleteShow(b: Boolean) {
        isShowDelete = b
        notifyDataSetChanged()
    }

    fun setDeleting(b: Boolean) {
        isDeleting = b
    }

    fun getDeleteShow(): Boolean {
        return isShowDelete
    }

    fun setOnItemDeleteListener(listener: OnItemDeleteListener?) {
        mOnItemDeleteListener = listener
    }

    override fun baseViewHolder(parent: ViewGroup): BaseViewHolder {
        val binding: ItemAppBinding = ItemAppBinding.inflate(LayoutInflater.from(context), parent, false)
        return ViewHolder(binding)
    }

    override fun bindItemView(viewHolder: BaseViewHolder?, position: Int, data: AppMode, total: Int) {
        viewHolder?.mBinding?.ivDelete?.visibility = View.GONE
        viewHolder?.mBinding?.ivImg?.setBackgroundResource(0)
        viewHolder?.mBinding?.ivImg?.setSkinImageResourcePadding(null, 0F)
        viewHolder?.mBinding?.ivImg?.setImageDrawable(null)

        if (!TextUtils.isEmpty(data.name)) {
            viewHolder?.mBinding?.tvName?.text = context?.let { AppFragment.getMenuName(it, data.id) }
            viewHolder?.mBinding?.ivImg?.setImageResource(MenuImageUtil.getMainImageId(context, data.id))
            if (data.icon == null) {  //显示卸载
                LogUtils.e("data.getIcon()=")
                viewHolder?.mBinding?.ivDelete?.visibility = View.GONE
            }
        } else if (!TextUtils.isEmpty(data.packageName) && !TextUtils.isEmpty(data.label)) {
            viewHolder?.mBinding?.tvName?.text = data.label
            viewHolder?.mBinding?.ivImg?.setImageDrawable(data.icon)
            if (isShowDelete && data.getFlags() and ApplicationInfo.FLAG_SYSTEM === 0) {
                LogUtils.e("PackageName=" + data.packageName)
                viewHolder?.mBinding?.ivDelete?.visibility = View.VISIBLE
            }
        }
        viewHolder?.itemView?.setOnClickListener { v ->
            if (isShowDelete) {
                //取消卸载
                isShowDelete = false
                notifyDataSetChanged()
            }
        }
        viewHolder?.mBinding?.ivImg?.setOnClickListener { v ->
            if (isShowDelete) {
                //取消卸载
                if (!TextUtils.isEmpty(data.packageName) && !isDeleting && data.getFlags() and ApplicationInfo.FLAG_SYSTEM === 0) {
                    //卸载
                    if (mOnItemDeleteListener != null) {
                        mOnItemDeleteListener!!.onDeleteApp(data, position)
                    }
                }
            } else mItemListener?.onItemClickListener(v, data, position)
        }
        viewHolder?.mBinding?.ivImg?.setOnLongClickListener { v ->
            if (data.id != AppFragment.ID_APP_STORE && data.id != AppFragment.ID_SETTING) {
                isShowDelete = true
                LogUtils.d("长按----")
                notifyDataSetChanged()
            }
            false
        }
        viewHolder?.mBinding?.ivDelete?.setOnClickListener { v ->
            //卸载应用
            if (!isDeleting && data.getFlags() and ApplicationInfo.FLAG_SYSTEM === 0) {
                //卸载
                if (mOnItemDeleteListener != null) {
                    mOnItemDeleteListener!!.onDeleteApp(data, position)
                }
            }
        }
    }


    interface OnItemDeleteListener {
        fun onDeleteApp(mode: AppMode, position: Int)
    }

    inner class ViewHolder(itemBinding: ItemAppBinding) : BaseViewHolder(itemBinding)
}